package com.ws.service.impl;

import com.github.pagehelper.Page;
import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.Supplier;
import com.ws.repository.SupplierRepository;
import com.ws.service.SupplierService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 11:13
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class SupplierServiceServiceImpl implements SupplierService {
    @Autowired
    SupplierRepository supplierRepository;

    @Override
    public PaasPage<Supplier> getSupplierPage(Pageable pageable, String supplierName,String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        Supplier supplier = new Supplier();
        supplier.setSupplierName(supplierName);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        supplier.setStatusCd(statusCd);
        Page<Supplier> supplierPage = supplierRepository.getSupplierPage(supplier);
        return PageUtil.transformPage(supplierPage);
    }

    @Override
    public Supplier saveSupplier(Supplier supplier) {
        if(null == supplier.getSupplierId()){
            supplier.setStatusCd(CommonConstant.ADD_STATUS_CD);
            supplier.setCreateTime(new Date());
            supplierRepository.saveSupplier(supplier);
        }else{
            supplierRepository.updateSupplier(supplier);
        }

        return supplier;
    }

    @Override
    public Supplier getSupplierBasicInfo(Long supplierId) {
        return supplierRepository.getSupplierBasicInfo(supplierId);
    }

    @Override
    public void delSupplier(Long supplierId) {
        supplierRepository.delSupplier(supplierId);
    }

    @Override
    public Supplier updateSupplier(Long supplierId, Supplier supplier) {
        supplierRepository.updateSupplier(supplier);
        return supplier;
    }

    @Override
    public List<Supplier> getSupplierList(String supplierName, String statusCd) {
        Supplier supplier = new Supplier();
        supplier.setSupplierName(supplierName);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        supplier.setStatusCd(statusCd);
        List<Supplier> supplierList = supplierRepository.getSupplierList(supplier);
        return supplierList;
    }
}
